$(function(){
	refresh();
});

var hasUserSelect = false;
function userSelect(){
	hasUserSelect = true;
}

function changeSummaryDate(){
	if (!hasUserSelect) return;
	hasUserSelect = false;
	refresh();
}

function refresh(summaryDateType, isForce) {
	var acountId = $("#acountId").combobox("getValue");
	if (acountId < 0) {
		return;
	}
	var summaryDate = $("#summaryDate").datebox("getValue");
	$.ajax("./detailreport.json", {
		mask : true,
		data : {
			summaryDate : summaryDate,
			summaryDateType : summaryDateType,
			isForce : isForce,
			acountId : acountId
		},
		success : loadData
	});
}

function loadData(data){
	if(data.bills && data.bills.length>0){
		$("#bill").datagrid("getPanel").show();
		$("#bill").datagrid("loadData",data.bills);
	} else {
		$("#bill").datagrid("getPanel").hide();
		$("#bill").datagrid("loadData",[]);
	}
	$("#dg").datagrid("loadData", []);
	$("#ff").form("clear");
	if(data.details){
		$("#dg").datagrid("loadData",data.details);
	}

	if(data.summary){
		$("#ff").form("load",data.summary);
		if(data.summary.runLog && data.summary.runLog!==""){
			$("#runLog").show();
		} else {
			$("#runLog").hide();
		}
	}
	$(window).resize();
}

function refreshForce() {
	refresh(null, true);
}

function changeAcount(newValue, oldValue) {
	if (oldValue && newValue) {
		frame.window.refresh({
			acountId : newValue
		});
	}
}

var autoRefreshForceJob;
function autoRefreshForce(){
	var checked = $("#autoRefreshForce").switchbutton("options").checked;
	if(checked){
		var interval = $("#autoRefreshForceInterval").numberbox("getValue");
		autoRefreshForceJob = setInterval("refreshForce()", interval*1000);
	} else {
		clearInterval(autoRefreshForceJob);
	}
}

function profitStyle(value,row,index){
	if (value > 0){
		return 'color:red;';
	} else if (value < 0){
		return 'color:green;';
	}
}